import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel {
	// private int mode; // 1 = texture; 2 =centre
//	private TexturePaint texture;
	private BufferedImage bufferedImage;
	public static final int CENTRE = 0;
	public static final int TEXTURE = 1;

	public Main(String fileName) {// , int mode) {
		super();
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		// this.mode = mode;
		this.bufferedImage = this.toBufferedImage(Toolkit.getDefaultToolkit()
				.getImage(fileName));
//		this.texture = new TexturePaint(bufferedImage, new Rectangle(0, 0,
//				bufferedImage.getWidth(), bufferedImage.getHeight()));
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		// switch (mode) {
		// case TEXTURE:
		// Graphics2D g2d = (Graphics2D) g;
		// g2d.setPaint(texture);
		// g2d.fillRect(0, 0, getWidth(), getHeight());
		// break;
		// case CENTRE:
		g.setColor(this.getBackground());
		g.fillRect(0, 0, getWidth(), getHeight());
//		g.drawImage(bufferedImage, (getWidth() - bufferedImage.getWidth()) / 2,
//				(getHeight() - bufferedImage.getHeight()) / 2, null);
		g.drawImage(bufferedImage, bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2, null);
		// break;
		// default:
		// super.paintComponents(g);
		// }
	}

	private BufferedImage toBufferedImage(Image image) {
		image = new ImageIcon(image).getImage();
		BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),
				image.getHeight(null), BufferedImage.TYPE_INT_RGB);
		Graphics g = bufferedImage.createGraphics();
		g.setColor(Color.white);
	//	g.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
		g.drawImage(image, 0, 0, null);
		g.dispose();
		return bufferedImage;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		RechercheFichier file = new RechercheFichier();
		ArrayList<String> ImgContent = file.getFileList();
		final JFrame fenetre = new JFrame();
		fenetre.pack();
		fenetre.setVisible(true);
		fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel toto = new JPanel (new GridLayout(2,1));
		JPanel tutu = new JPanel(new FlowLayout());
		JButton fermer = new JButton("fermer");
		fermer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fenetre.dispose();
			}
		});
		fenetre.setContentPane(toto);
		tutu.add(fermer);
		int i = 0;
		while (i < ImgContent.size()) {
			fenetre.setSize(600, 500);
			fenetre.setLocation(100, 100);
			Main mm = new Main(ImgContent.get(i)); // , 2);
			// Diapo dia = new Diapo(ImgContent.get(i));
			toto.add(mm, BorderLayout.NORTH);
			toto.add(tutu, BorderLayout.SOUTH);
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			fenetre.remove(mm);
			fenetre.repaint();
			fenetre.pack();
			i++;
		}
	}
}
